﻿
/*
ESOE-IDE class: RuleCompiler

Author: Feng Weiguo ( 冯卫国 )
Mail: forxm@21cn.com
Web: www.supertree.org

All Rights Reserved, 2008-9

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
*/

$esoe.$preload(
	"org.supertree.editor.RuleCompiler",
	function()
	{
		throw "can not instantiate RuleCompiler";
	},
	function()
	{
		this.Compile= function( rule )
		{
			var r,i;
			
			if( rule[0] && typeof(rule[0])=="string" )
			{
				r= new RegExp;
				r.compile( rule[0], "g" );
				rule[0]= r;
			}
			else return;
			
			for(i=1;i<rule.length;i++)
			{
				if( rule[i][2]>0 )
				{
					if( typeof(rule[i][4])=="string" ) rule[i][4]=eval(rule[i][4]);
					this.Compile( rule[i][4] );
				}
			}
		}
		
		//format a string to IgnoreCase reg string, ex, abc=>[aA][aB][cC]
		this.ToIgnoreCase= function(str)
		{
			var s1=str.toLowerCase();
			var s2=str.toUpperCase();
			var i,imax;
			imax=s1.length;
			var sa=new Array;
			for(i=0;i<imax;i++)
			{
				if( s1.charAt(i)!=s2.charAt(i) )
				{
					sa.push( "["+s1.charAt(i)+s2.charAt(i)+"]");
				}
				else sa.push(s1.charAt(i));
			}
			//alert(sa.join(""));
			return sa.join("");
		}
		
		//default color style
		this.aColor=["Red","Blue","Green","Gray","Brown","Fuchsia","Purple"];
		this.aColor.sort();	//for removing duplicated
		
		this.add_rule= function(sh,sel, s )
		{
			if(window.ActiveXObject)
			{
				//sh.addRule(sel,s,sh.rules.length);	//Any unsupported sel in addRule will crash IE 6
				sh.cssText+= sel+"{"+s+"}";	//for ie6, more safe version
			}
			else sh.insertRule(sel+"{"+s+"}",0);
		}
		
		//creat color style
		//doc: the window.document
		this.ColorStyle= function( a, doc )
		{
			var i;
			var ac=new Array;	//color list
			var sheet=(doc?doc:document).styleSheets[0];
			
			var sB= "font-weight:bold;letter-spacing:0px;"+(window.ActiveXObject?"*padding-right:1px;":"");
			var sU= "text-decoration:underline;";
			var sRed= "color:red;";
			
			for(i=0;i<a.length;i++)
			{
				if(i>0 && a[i]==a[i-1]) continue;	//remove duplicated
				
				ac[ac.length]=a[i];
				this.add_rule(sheet, "."+a[i],"color:"+a[i]+";");
				this.add_rule(sheet, "."+a[i]+"B","color:"+a[i]+";"+sB);
				this.add_rule(sheet, "."+a[i]+"U","color:"+a[i]+";"+sU);
				this.add_rule(sheet, "."+a[i]+"BU","color:"+a[i]+";"+sU+sB);
			}
			
			if(a==this.aColor)	//only 1 time
			{
				this.add_rule(sheet, ".err",sRed+sB);
				this.add_rule(sheet, ".errx",sRed+sB);
				this.add_rule(sheet, ".alert",sRed);
				
				this.add_rule(sheet, ".trace",sRed+sB+sU);
				this.add_rule(sheet, ".trace2",sRed+sB+"text-decoration:line-through;");
				this.add_rule(sheet, ".trace3",sRed+sB+"text-decoration:overline;");
			}
		}
		
		//aaa
		this.ColorStyle(this.aColor);
		
		//void CSS to ensure app containing at least 1 style
		
		//#ESOE_RES
		//#CSS: css_RuleCompiler, RuleCompiler.css
		//#END_ESOE_RES
		
	}
	);
